package com.redoor.app.stake.basicdata.controller;

import com.redoor.app.stake.auth.model.serviceModel.LoginUserBean;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.model.UsrDistrict;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.redoor.app.stake.basicdata.model.SyncOrganization;
import com.redoor.app.stake.basicdata.model.controllerModel.SyncOrganizationCriteria;
import com.redoor.app.stake.basicdata.service.SyncOrganizationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Created by wangjunlei on 2018-03-05 17:11:50.
*/
@RestController
@RequestMapping("/sync/organizations")
public class SyncOrganizationController {

    @Autowired
    private SyncOrganizationService syncOrganizationService;
    @Autowired
    private BaseUserService baseUserService;
    @DeleteMapping("/{orgno}")
    public ResponseResult delete(@PathVariable String orgno) {
        syncOrganizationService.deleteByOrgNo(orgno);
        return ResultGenerator.generatorSuccess();
    }

    @PutMapping
    public ResponseResult update(@RequestBody SyncOrganizationCriteria syncOrganizationCriteria) {
        syncOrganizationService.updateByOrgNo(syncOrganizationCriteria);
        return ResultGenerator.generatorSuccess();
    }

    @GetMapping("/{orgno}")
    public ResponseResult detail(@PathVariable String orgno) {
        SyncOrganization syncOrganization = syncOrganizationService.get(orgno);
        return ResultGenerator.generatorSuccess(syncOrganization);
    }

    @GetMapping("/findByParentOrgNo")
    public ResponseResult list(String parentcode) {
        List<SyncOrganization> list = syncOrganizationService.findByParentOrgNo(parentcode);
        return ResultGenerator.generatorSuccess(list);
    }

    @GetMapping("/getAllList")
    public ResponseResult getAllList(String status) {
        List<SyncOrganization>  lists = new ArrayList<SyncOrganization>();
        //获取登录用户的组织机构列表
        List<String> loginUserOrgList = this.baseUserService.districtPermission();
        if("1".equals(status) && !CollectionUtils.isEmpty(loginUserOrgList)){
            for(int i=0; i< loginUserOrgList.size(); i++){
                //根据orgNo查询对象
                SyncOrganization organization = syncOrganizationService.get(loginUserOrgList.get(i));
                if(null == organization){
                    continue;
                }
                //如果是查询当前用户的组织机构树,因为可能是不完整的树形,防止页面出错,强制设置父级为3,保证树形结构健全,注意jstree树形必须以#开始
                organization.setParentorgno("3");
                lists.add(organization);

                List<SyncOrganization> list = syncOrganizationService.findByParentOrgNo(loginUserOrgList.get(i));
                lists.addAll(list);
            }
        } else {
            lists = syncOrganizationService.getAllList(null,true);
        }
        return ResultGenerator.generatorSuccess(lists);
    }

    @GetMapping("/initOrganizationToRedis")
    public ResponseResult initRedis(boolean isDistraint) {
        syncOrganizationService.initOrganizationToRedis(isDistraint);
        return ResultGenerator.generatorSuccess();
    }

    @GetMapping("/fullOrgName")
    public ResponseResult fullOrgName(String orgcode) {
        String fullOrgName = syncOrganizationService.getFullOrgName(orgcode);
       return ResultGenerator.generatorSuccess(fullOrgName);
    }

    @GetMapping("/listUsrDistrictAll")
    public ResponseResult listUsrDistrictAll() {
        List<UsrDistrict> list = syncOrganizationService.listUsrDistrictAll();
        return ResultGenerator.generatorSuccess(list);
    }
}
